package gui;

import java.awt.Color;
import java.awt.Label;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderPanel extends JPanel implements ChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Object context;
	private String methodName;

	public SliderPanel(String label, int min, int max, int init,
			Object o, String m) {

		this.context = o;
		this.methodName = m;

		setBorder(BorderFactory.createLineBorder(Color.black));
		add(new Label(label));
		JSlider slider = new JSlider(min, max, init);
		add(slider);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.addChangeListener(this);

	}

	public void stateChanged(ChangeEvent e) {
		JSlider source = (JSlider)e.getSource();

		try {

			Class<?>[] params = {float.class, boolean.class};
			Method setMethod = context.getClass().getMethod("set".concat(methodName), params);
			setMethod.invoke(context, (float)source.getValue(), false);
			
		} catch (SecurityException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		}catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		}
}
}
